package br.museu.dao.basic;

import java.util.List;

import org.hibernate.Query;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.springframework.stereotype.Repository;

import br.museu.dao.basic.intefaces.ITicketDAO;
import br.museu.dao.generico.DAOGenerico;
import br.museu.model.Evento;
import br.museu.model.Ticket;
import br.museu.model.Usuario;

@Repository
public class TicketDAO extends DAOGenerico<Ticket> implements ITicketDAO {

	public TicketDAO() {
	}

	public void comprarTicket(List<Ticket> tickets) {

		this.inserirColecao(tickets);

	}

	@SuppressWarnings("unchecked")
	public List<Ticket> listarTicketsEvento(Evento evento) {

		String strQuery = "Select t from Ticket t Where t.evento = :evento";

		Query query = getCurrentSession().createQuery(strQuery);
		query.setParameter("evento", evento);

		return query.list();
	}

	public void cancelarCompraTicket(Ticket ticket) {
		this.remover(ticket);
	}

	public Ticket consultarPorCodigo(String codigo) {
		String strQuery = "Select t from Ticket t Where t.codigo = :codigo";

		Query query = getCurrentSession().createQuery(strQuery);
		query.setParameter("codigo", codigo);

		return (Ticket) query.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<Ticket> listarTicketsVendidosMes(LocalDate data) {
		String strQuery = "Select t From Ticket t where month(t.dtEmissao) = :mes order by day(t.dtEmissao)";

		Query query = getCurrentSession().createQuery(strQuery);
		query.setInteger("mes", data.getMonthOfYear());

		return query.list();
	}

	@SuppressWarnings("unchecked")
	public List<Ticket> listarTicketsVendidosDia() {

		DateTime today = new DateTime().withTimeAtStartOfDay();
		DateTime tonight = today.plusDays(1);

		String strQuery = "Select t From Ticket t where t.dtEmissao >= :today And t.dtEmissao <= :tonight";
		Query query = getCurrentSession().createQuery(strQuery);
		query.setTimestamp("today", today.toDate());
		query.setTimestamp("tonight", tonight.toDate());

		return query.list();
	}

	@SuppressWarnings("unchecked")
	public List<Ticket> listarTicketsUsuario(Usuario usuario) {
		String strQuery = "Select t From Ticket t where t.usuario = :usuario";

		Query query = getCurrentSession().createQuery(strQuery);
		query.setParameter("usuario", usuario);

		return query.list();
	}

}
